﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace EntityFrameworks.Entity.Core
{
    /// <summary>
    /// 实体基类，与业务和架构无关
    /// </summary>
    public abstract class EntityBase
    {

        // Constructors
        public EntityBase()
        {
        }

        #region Properties
        /// <summary>
        /// 数据验证(是否成功)
        /// 虚属性，子类可以根据自己的逻辑去复写
        /// </summary>
        public virtual bool IsValid
        {
            get
            {
                return this.GetRuleViolations() == null
                    || this.GetRuleViolations().Count() == 0;
            }
        }

        #endregion

        #region Methods
        /// <summary>
        /// 获取验证失败的信息枚举,默认提供了非空验证
        /// 它使用了简单的迭代器,如果GetRuleViolations有错误则返回迭代列表
        /// </summary> 
        /// <returns></returns>
        public virtual IEnumerable<RuleViolation> GetRuleViolations()
        {
            return null;
        }
        #endregion

        #region Events
        public event Action<SavedEventArgs> SaveBefore;
        public event Action<SavedEventArgs> SaveAfter;


        /// <summary>
        /// 插入前执行
        /// </summary>
        public void OnSaveBefore(SavedEventArgs e)
        {
            if (SaveBefore != null)
                this.SaveBefore(e);
        }
        /// <summary>
        /// 插入后执行
        /// </summary>
        public void OnSaveAfter(SavedEventArgs e)
        {
            if (SaveAfter != null)
                this.SaveAfter(e);
        }


        #endregion
    }


}
